package zpx.dao;


import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import zpx.idao.IComAttachmentDao;
import zpx.model.ComAttachment;
import zpx.model.TableUtil;


/**
 * 锟斤拷锟斤拷锟斤拷菘锟斤拷锟斤拷锟接匡拷实锟斤拷锟斤拷
 */

@Repository("comAttachmentDao")
public class ComAttachmentDao extends HibernateDaoSupport implements  IComAttachmentDao {
    
   
	@Resource
	public void setSuperSessionFactory(SessionFactory sessionFactory) {
		this.setSessionFactory(sessionFactory);
	}
	
    
    /**
     * 锟斤拷荻锟接︼拷锟斤拷锟斤拷录锟斤拷锟斤拷锟绞讹拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟�
     * 
     * @param recordId 锟斤拷应锟斤拷锟斤拷锟铰硷拷锟斤拷锟斤拷识
     * @return 锟斤拷锟斤拷锟叫憋拷
     */
    public List<ComAttachment> getAttachments(String recordId) {
        if (recordId == null || "".equals(recordId.trim())) {
            return null;
        }
        // 锟斤拷荻锟接︼拷锟斤拷锟斤拷录锟斤拷锟斤拷锟绞讹拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟�
        @SuppressWarnings("unchecked")
        
        List<ComAttachment> attachments = (List<ComAttachment>) getHibernateTemplate().find(
                "from  ComAttachment where recordId = ? and attaStatus=1", recordId.trim());
        if (attachments == null || attachments.isEmpty()) {
            return null;
        } else {
            return  attachments;
        }
        /*// 锟斤拷锟絊ession
        Session session = this.getSessionFactory().getCurrentSession();
        // 锟斤拷锟紺riteria
        Criteria criteria = session.createCriteria(ComAttachment.class);
        // 锟斤拷锟斤拷约锟斤拷锟斤拷锟斤拷
        criteria.add(Restrictions.eq("recordId", recordId)).add(Restrictions.eq("attaStatus", "1"));
        return criteria.list();*/
        
    }
    
    
    /**
     * 锟斤拷锟斤拷锟斤拷锟斤拷锟窖�
     * @param searchMap
     * @return
     */
    public List<ComAttachment> getAttachmentsLst(Map<String, String> searchMap){
       
        // 锟斤拷荻锟接︼拷锟斤拷锟斤拷录锟斤拷锟斤拷锟绞讹拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟�
        @SuppressWarnings("unchecked")
        // 锟斤拷锟絊ession
        Session session = this.getSessionFactory().getCurrentSession();
        // 锟斤拷锟紺riteria
        Criteria criteria = session.createCriteria(ComAttachment.class);
        // 锟斤拷锟斤拷约锟斤拷锟斤拷锟斤拷
        String recordId = searchMap.get("recordId");
        if(recordId != null && !"".equals(recordId)){
            criteria.add(Restrictions.eq("recordId", recordId));
        }
        String remark = searchMap.get("remark");
        if(remark != null && !"".equals(remark)){
            criteria.add(Restrictions.eq("remark", remark));
        }
        String attaGroupId = searchMap.get("attaGroupId");
        if(attaGroupId != null && !"".equals(attaGroupId)){
            criteria.add(Restrictions.eq("attaGroupId", attaGroupId));
        }
        String attaName = searchMap.get("attaName");
        if(attaName != null && !"".equals(attaName)){
            criteria.add(Restrictions.like("attaName", "%" + attaName + "%"));
        }
        criteria.add(Restrictions.eq("attaStatus", "1"));
        return criteria.list();
    }
    
    /**
     * 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷取
     * 
     * @param attaId 锟斤拷锟斤拷锟侥硷拷锟斤拷识
     * @return 锟斤拷锟斤拷
     */
    public ComAttachment getAttachment(String attaId) {
        
        if (attaId == null || "".equals(attaId.trim())) {
            return null;
        }
        // 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷取
        @SuppressWarnings("unchecked")
        List<ComAttachment> attachments = (List<ComAttachment>) getHibernateTemplate().find(
                "from  ComAttachment where attaId = ? ", attaId.trim());
        if (attachments == null || attachments.isEmpty()) {
            return null;
        } else {
            return (ComAttachment) attachments.get(0);
        }
    }
    
    /**
     * 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
     * 
     * @param attachment 锟斤拷锟斤拷锟斤拷锟斤拷
     * @return 锟斤拷锟斤拷锟斤拷锟斤拷
     */
    public ComAttachment saveOrUpdateAttachment(ComAttachment attachment) {
        
        // 锟铰斤拷锟斤拷锟斤拷时锟斤拷取锟铰碉拷锟斤拷锟斤拷
        if (attachment.getAttaId() == null || "".equals(attachment.getAttaId())) {
            String attaId = TableUtil.getPK("EX");
            attachment.setAttaId(attaId);
        }
        // 锟斤拷锟斤拷锟斤拷锟铰诧拷锟斤拷
        getHibernateTemplate().saveOrUpdate(attachment);
        getHibernateTemplate().flush();
        return attachment;
    }
    
    /**
     * 删锟斤拷锟�
     * 
     * @param attaId 锟斤拷锟斤拷锟侥硷拷锟斤拷识
     * @return 锟斤拷锟斤拷锟斤拷锟斤拷
     */
    public void deleteAttachment(String attaId) {
        if(attaId==null || "".equals(attaId)){
            return;
        }
        StringBuffer sql = new StringBuffer();
        sql.append("DELETE  from t_attachment WHERE attaId =:attaId ");
        SQLQuery query = this.getSessionFactory().getCurrentSession().createSQLQuery(sql.toString());
        query.setParameter("attaId", attaId);
        query.executeUpdate();
        
        //getHibernateTemplate().delete("delete ComAttachment where attaId = ? ", attaId.trim());
    }
    
    /**
     * 
     * @param recordId 锟斤拷锟斤拷锟侥硷拷锟斤拷应锟斤拷锟绞�
     * @param group 锟斤拷锟�
     * @return 锟斤拷锟斤拷锟叫憋拷
     */
    public List<ComAttachment> getComAttachmentList(String recordId, String group) {
        if (recordId == null || "".equals(recordId.trim())) {
            return null;
        }
        Session session = this.getSessionFactory().getCurrentSession();
        Criteria criteria = session.createCriteria(ComAttachment.class);
        criteria.add(Restrictions.eq("recordId", recordId))
                .add(Restrictions.eq("attaGroupId", group)).add(Restrictions.eq("attaStatus", "1"));
        List<ComAttachment> attaList = criteria.list();
        return attaList;
    }
}
